/**
 * FileName: CheckLonLatTimer
 * Author:   Leopold
 * Date:     2018/9/19 15:18
 * Description:
 * History:
 */
package cn.mcsys.worker.core.timer;

import cn.mcsys.myadmin.common.utils.MapUtils;
import cn.mcsys.myadmin.dubbo.service.BackService;
import cn.mcsys.myadmin.modules.air.entity.AirCar;
import cn.mcsys.myadmin.modules.air.entity.AirRidePoint;
import cn.mcsys.myadmin.modules.worker.entity.AirTaxi;
import cn.mcsys.worker.core.conf.LoadRidePointConfiguration;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.awt.geom.Point2D;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Component
//@EnableScheduling
//@Lazy(false)
public class CheckLonLatTimer {


    @Autowired
    private BackService backService;

    /**
     * 届时改成  airCar
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void sayPoint() {
//        查询前20秒的gps数据
        Date currentDate = new Date();
        Date beforeDate = DateUtils.addSeconds(currentDate, -20);
        List<AirTaxi> airTaxis = backService.select20sGPS(currentDate, beforeDate);
        Set<Map.Entry<String, List<Point2D.Double>>> entries = LoadRidePointConfiguration.ridePointMap.entrySet();

        for (AirTaxi airTaxi : airTaxis) {
            String lon = airTaxi.getLon();
            String lat = airTaxi.getLat();
            Point2D.Double point2D = new Point2D.Double(Double.valueOf(lon), Double.valueOf(lat));
//                首先判断是否偏移线路了
//                设置偏移异常

//            查询出该车辆的所属的乘车点
//            判断是否在乘车点或者停车场,判断是否在线路上
            String vehicleNo = airTaxi.getVehicleNo();
            AirCar airCar = backService.selectByCarNumber(vehicleNo);
            AirRidePoint airRidePoint = airCar.getAirRidePoint();
            String name = airRidePoint.getName();
            List<Point2D.Double> pointLonLat = getPointLonLat(name);
            String pointLon = airRidePoint.getLon();
            String pointLat = airRidePoint.getLat();
            //判断是否在乘车点上
            boolean isPtInPoly = MapUtils.IsPtInPoly(new Point2D.Double(Double.valueOf(pointLon), Double.valueOf(pointLat)), pointLonLat);
            //判断是否在线路上
            //判断是否在停车场上
            //如果都不在就报车辆偏移

        }
    }


    private List<Point2D.Double> getPointLonLat(String name) {
        switch (name) {
            case "香洲总站":
                return LoadRidePointConfiguration.ridePointMap.get("xiangzhou");
            case "城轨珠海站":
                return LoadRidePointConfiguration.ridePointMap.get("chenggui");
            case "华发商都":
                return LoadRidePointConfiguration.ridePointMap.get("huafashangdou");
            case "九洲城":
                return LoadRidePointConfiguration.ridePointMap.get("jiuzhoucheng");
            case "九州港":
                return LoadRidePointConfiguration.ridePointMap.get("jiuzhouguang");
            case "国际赛车场":
                return LoadRidePointConfiguration.ridePointMap.get("saichechang");
            case "斗门客运站":
                return LoadRidePointConfiguration.ridePointMap.get("doumen");
            case "前山枢纽站":
                return LoadRidePointConfiguration.ridePointMap.get("qianshan");
            case "体育中心东门":
                return LoadRidePointConfiguration.ridePointMap.get("tiyuzhongxin");
        }
        return null;
    }

//        List<AirRidePoint> points = backService.selectAllPoint();
//        System.out.println(points);
}
